package cn.kgc.controller;

import cn.kgc.pojo.SmbmsUser;
import cn.kgc.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class UserController {
    private Logger logger = Logger.getLogger(UserController.class);

@Resource
    private UserService service;
    @RequestMapping("/")
    public String toLogin(){
        return "login";
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestParam(value = "userCode",required = false)String userCode,
                        @RequestParam(value = "userPassword",required = false)String userPassword,
                        HttpServletRequest request,
                        Model model){
        logger.info("userCode===="+userCode+"userPassword===="+userPassword);
        SmbmsUser smbmsUser = service.getDengLu(userCode,userPassword);
        if(smbmsUser != null){
            HttpSession session = request.getSession();
            session.setAttribute("user",smbmsUser);
            return "redirect:/user/";
        }else {
            model.addAttribute("error","用户名或者密码错误");
            return "login";
        }
    }
}
